package com.example.spring_redis_demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class MyController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/testString")
    @ResponseBody
    public String testString(){
        redisTemplate.opsForValue().set("key1","111");
        redisTemplate.opsForValue().set("key2","222");
        redisTemplate.opsForValue().set("key3","333");

        String value = redisTemplate.opsForValue().get("key1");
        System.out.println("value : "+value);

        return "OK";
    }

    @GetMapping("/testList")
    @ResponseBody
    public String testList(){
        //清除之前的数据
        redisTemplate.execute((RedisConnection connection)->{

            //execute 要求回调方法中必须写return 语句，返回个东西
            // 这个回调返回的对象，就会作为execute 本身的返回值。
            connection.flushAll();
            return null;
        } );

        redisTemplate.opsForList().leftPush("key","111");
        redisTemplate.opsForList().leftPush("key","222");
        redisTemplate.opsForList().leftPush("key","333");

        String value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:"+value);

        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:"+value);
        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value:"+value);

        return "OK";
    }

    @GetMapping("/testSet")
    @ResponseBody
    public String testSet(){
        //清除之前的数据
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        } );

        redisTemplate.opsForSet().add("key","111","222","333");
        Set<String> ret = redisTemplate.opsForSet().members("key");
        System.out.println("ret:"+ret);

        boolean exists = redisTemplate.opsForSet().isMember("key","111");
        System.out.println("exists:"+exists);

        long count = redisTemplate.opsForSet().size("key");
        System.out.println("count:"+count);

        redisTemplate.opsForSet().remove("key","111","333");

        count = redisTemplate.opsForSet().size("key");
        System.out.println("after remove count:"+count);
        return "OK";
    }

    @GetMapping("/testHash")
    @ResponseBody
    public String testHash(){
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return  null;
        });

        redisTemplate.opsForHash().put("key","f1","111");
        redisTemplate.opsForHash().put("key","f2","222");
        redisTemplate.opsForHash().put("key" ,"f3","333");

        String value = (String) redisTemplate.opsForHash().get("key","f1");
        System.out.println("value:"+value);

        long size = redisTemplate.opsForHash().size("key");
        System.out.println("size:"+size);

        Boolean exists = redisTemplate.opsForHash().hasKey("key","f1");
        System.out.println("f1 exists:"+exists);
        exists =  redisTemplate.opsForHash().hasKey("key","f5");
        System.out.println("f5 exists:"+exists);

        redisTemplate.opsForHash().delete("key","f3","f2");
        size = redisTemplate.opsForHash().size("key");
        System.out.println("after delete size:"+size);


        return "OK";
    }

    @GetMapping("/testZset")
    @ResponseBody
    public String testZset(){
        redisTemplate.execute((RedisConnection connection)->{
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key","zhangsan",10);
        redisTemplate.opsForZSet().add("key","lisi",20);
        redisTemplate.opsForZSet().add("key","wangwu",30);

        Set<String> members = redisTemplate.opsForZSet().range("key",0,-1);
        System.out.println("member:"+members);

        Set<ZSetOperations.TypedTuple<String>> memberswithscore = redisTemplate.opsForZSet().rangeWithScores("key",0,-1);
        System.out.println("memberswithscore:" + memberswithscore);

        Double score = redisTemplate.opsForZSet().score("key","zhangsan");
        System.out.println("zhangsan score:"+score);

        long size = redisTemplate.opsForZSet().size("key");
        System.out.println("size : "+size);

        long No =  redisTemplate.opsForZSet().rank("key","lisi");
        System.out.println("lisi 1:" + No);

        redisTemplate.opsForZSet().remove("key","zhangsan");

        size = redisTemplate.opsForZSet().size("key");
        System.out.println("size : "+size);

        No =  redisTemplate.opsForZSet().rank("key","lisi");
        System.out.println("lisi 0:" + No);

        return "OK";
    }
}
